package com.woniuxy.third.interceptor;

import com.woniuxy.third.annotation.RequirePermission;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.SubstituteLogger;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

public class PermsInterceptor extends HandlerInterceptorAdapter {
    LoggerFactory loggerFactory;
    SubstituteLogger substLogger;
    private static final Logger logger = LoggerFactory.getLogger(PermsInterceptor.class);
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.debug("拦截请求");

        //1.得到即将被访问的方法
        //如果是请求的handler中的方法
        if (handler instanceof HandlerMethod){
            logger.debug("获取被请求的方法");
            HandlerMethod handlerMethod = (HandlerMethod)handler;
            //
            Method method = handlerMethod.getMethod();
            logger.debug("判断方法上是否有指定的注解");
            if (method.isAnnotationPresent(RequirePermission.class)){
                logger.debug("方法上有注解,得到注解对象");
                RequirePermission rp = method.getDeclaredAnnotation(RequirePermission.class);
                logger.debug("获取注解中的值");
                String perms = rp.value();
                logger.debug("value = " + perms);
            }
        }

        return super.preHandle(request, response, handler);
    }
}
